home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / hpgl2ps.zip / HPGLCOM.C < prev    next >
C/C++ Source or Header  |  1989-08-08  |  13KB  |  626 lines

  1. /* hpglcom.c */
  2. /*
  3.  * This procedure translates RD-GL (Roland DG Graphic Language) into the
  4.  * equivalent PostScript language. 
  5.  *
  6.  * The RD-GL is a superset equivalent to HP-GL 
  7.  *
  8.  * Don McCormick 
  9.  */
  10.  
  11. #include "defn.h"
  12.  
  13. /* The folowing defaults should be 0.5% and 1.0% for the respective  character
  14.  * width and height, however this is too small when scaled to Postcript
  15.  * charcter sizes.
  16.  */
  17. float DEFWIDTH = 0.0075;    /* 0.75 % of P2x - P1x for default char width */
  18. float DEFHEIGHT = 0.015;    /* 1.5 % of P2y - P1y for default char height */
  19. /*
  20.  * Values above are further multiplied by FONT_W_MULT and FONT_H_MULT
  21.  * which can be set on command line. Defaults are 4.0 and 1.1
  22.  * Added for better matching of relative font sizes to plotter results 
  23.  */
  24.  
  25. #define SPACE_FACTOR 0.64  /* used in computation of a character space */
  26.  
  27. hpglcom(op1)
  28. char    op1;
  29. {
  30.     char    op2;
  31.  
  32.     switch (op1)
  33.     {
  34.     case 'A':
  35.     case 'a':
  36.     op2 = getc(stream);
  37.     switch (op2)
  38.     {
  39.     case 'A':        /* Arc Absolute (not HP-GL) */
  40.     case 'a':
  41.         break;
  42.  
  43.     case 'P':        /* Automatic Pen Lift (HP-GL only) */
  44.     case 'p':
  45.         while (((ch = getc(stream)) != EOF) && ch != ';');
  46.         break;
  47.  
  48.     case 'R':        /* Arc Relative (not HP-GL) */
  49.     case 'r':
  50.         break;
  51.  
  52.     default:
  53.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n\n", op1, op2);
  54.     }
  55.     break;
  56.  
  57.     case 'C':
  58.     case 'c':
  59.     op2 = getc(stream);
  60.     switch (op2)
  61.     {
  62.     case 'A':        /* Alternate Character Set (Not Used) */
  63.     case 'a':
  64.         while (((ch = getc(stream)) != EOF) && ch != ';');
  65.         fprintf(stderr, "Warning: Alt character set not implemented yet\n");
  66.         break;
  67.  
  68.     case 'I':        /* Circle */
  69.     case 'i':
  70.         circle(RDGLCIRCLE);
  71.         break;
  72.  
  73.     case 'P':        /* Character Plot */
  74.     case 'p':
  75.         {
  76.         float   xspace, yspace;
  77.  
  78.         xspace = getval() * XSCALE * SCALE * (char_width + char_space);
  79.         yspace = getval() * YSCALE * SCALE * (char_width + char_space);
  80.         end_draw();
  81.         printf("    %g mm %g mm %s\n", xspace, yspace, RMOVE);
  82.         }
  83.         break;
  84.  
  85.     case 'S':        /* Standard Character Set */
  86.     case 's':
  87.         while (((ch = getc(stream)) != EOF) && ch != ';');
  88.         break;
  89.  
  90.     default:
  91.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  92.     }
  93.     break;
  94.  
  95.     case 'D':
  96.     case 'd':
  97.     op2 = getc(stream);
  98.     switch (op2)
  99.     {
  100.     case 'C':        /* Digitize Clear (Not Used) */
  101.     case 'c':
  102.         break;
  103.  
  104.     case 'F':        /* Default */
  105.     case 'f':
  106.         SETDOT = 0;
  107.         SYMBOL = 0;
  108.         PENDOWN = 0;
  109.         EOL = '\003';
  110.         char_angle = 0;
  111.         char_slant = 0;
  112.         if(LANDSCAPE) {
  113.         char_width = DEFWIDTH * (ymax - ymin) * YSCALE *  SCALE;
  114.         char_height = DEFHEIGHT * (xmax - xmin) * XSCALE *  SCALE;
  115.         }
  116.         else {
  117.         char_width = DEFWIDTH * (xmax - xmin) * XSCALE *  SCALE;
  118.         char_height = DEFHEIGHT * (ymax - ymin) * YSCALE *  SCALE;
  119.         }
  120.         char_space = char_width * (1/SPACE_FACTOR - 1);
  121.         printf("/%s %g %g %g DefFont\n",
  122.         font, char_width, char_height, char_slant);
  123.         break;
  124.  
  125.     case 'I':        /* Absolute Direction */
  126.     case 'i':
  127.         {
  128.         float   run, rise;
  129.  
  130.         if (SIGNED_NUMERIC)
  131.         {
  132.             run = getval() * XSCALE;
  133.             rise = getval() * YSCALE;
  134.             char_angle = atan2(rise, run) / deg_rad;
  135.         }
  136.         else
  137.             char_angle = 0;
  138.         }
  139.         break;
  140.  
  141.     case 'P':        /* Digitize Point (Not Used) */
  142.     case 'p':
  143.         break;
  144.  
  145.     case 'R':        /* Relative Direction */
  146.     case 'r':
  147.         {
  148.         float   run, rise;
  149.  
  150.         if (SIGNED_NUMERIC)
  151.         {
  152.             run = getval() * XSCALE;
  153.             rise = getval() * YSCALE;
  154.             char_angle += atan2(rise, run) / deg_rad;
  155.         }
  156.         else
  157.             char_angle = 0;
  158.         }
  159.         break;
  160.  
  161.     case 'T':        /* Define Terminator */
  162.     case 't':
  163.         if ((ch = getc(stream)) != EOF)
  164.         EOL = ch;    /* End of label terminator */
  165.         break;
  166.  
  167.     default:
  168.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  169.     }
  170.     break;
  171.  
  172.     case 'E':            /* NOT HP-GL */
  173.     case 'e':
  174.     op2 = getc(stream);
  175.     switch (op2)
  176.     {
  177.     case 'A':        /* Edge Rectangle Absolute */
  178.     case 'a':
  179.         break;
  180.  
  181.     case 'R':        /* Edge Rectangle Relative */
  182.     case 'r':
  183.         break;
  184.  
  185.     case 'W':        /* Edge Wedge */
  186.     case 'w':
  187.         break;
  188.  
  189.     default:
  190.         fprintf(stderr, "Warning: %c%c Unknown RD-GL Command\n", op1, op2);
  191.     }
  192.     break;
  193.  
  194.     case 'F':            /* NOT HP-GL */
  195.     case 'f':
  196.     op2 = getc(stream);
  197.     switch (op2)
  198.     {
  199.     case 'T':        /* Fill Type */
  200.     case 't':
  201.         break;
  202.  
  203.     default:
  204.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  205.     }
  206.     break;
  207.  
  208.     case 'I':
  209.     case 'i':
  210.     op2 = getc(stream);
  211.     switch (op2)
  212.     {
  213.     case 'M':        /* Input Mask (Not Used) */
  214.     case 'm':
  215.         break;
  216.  
  217.     case 'N':        /* Initialize */
  218.     case 'n':
  219.         plotcoords();
  220.         SETDOT = 0;
  221.         SYMBOL = 0;
  222.         PENDOWN = 0;
  223.         EOL = '\003';
  224.         char_angle = 0;
  225.         char_slant = 0;
  226.         if(LANDSCAPE) {
  227.         char_width = DEFWIDTH * (ymax - ymin) * YSCALE *  SCALE;
  228.         char_height = DEFHEIGHT * (xmax - xmin) * XSCALE * SCALE;
  229.         }
  230.         else {
  231.         char_width = DEFWIDTH * (xmax - xmin) * XSCALE *  SCALE;
  232.         char_height = DEFHEIGHT * (ymax - ymin) * YSCALE * SCALE;
  233.         }
  234.         char_space = char_width * (1/SPACE_FACTOR - 1);
  235.         printf("/%s %g %g %g DefFont\n",
  236.         font, char_width, char_height, char_slant);
  237.         break;
  238.  
  239.     case 'P':        /* Input P1 and P2 (Not Used) */
  240.     case 'p':
  241.         while (((ch = getc(stream)) != EOF) && ch != ';');
  242.         fprintf(stderr,"Warning: IP command not implemented\n");
  243.         break;
  244.  
  245.     case 'W':        /* Input Window */
  246.     case 'w':
  247.         while (((ch = getc(stream)) != EOF) && ch != ';');
  248.         fprintf(stderr,"Warning: IW command not implemented\n");
  249.         break;
  250.  
  251.     default:
  252.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  253.     }
  254.     break;
  255.  
  256.     case 'L':
  257.     case 'l':
  258.     op2 = getc(stream);
  259.     switch (op2)
  260.     {
  261.     case 'B':        /* Label */
  262.     case 'b':
  263.         textps(TEXT);
  264.         break;
  265.  
  266.     case 'T':        /* Line Type */
  267.     case 't':
  268.         linetype(LINE_TYPE_SCALE);
  269.         break;
  270.  
  271.     default:
  272.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  273.     }
  274.     break;
  275.  
  276.     case 'O':            /* NOT USED */
  277.     case 'o':
  278.     op2 = getc(stream);
  279.     switch (op2)
  280.     {
  281.     case 'A':        /* Output Actual Position (Not HP-GL) */
  282.     case 'a':
  283.         break;
  284.  
  285.     case 'C':        /* Output Commanded Position */
  286.     case 'c':
  287.         break;
  288.  
  289.     case 'D':        /* Output Digitise */
  290.     case 'd':
  291.         break;
  292.  
  293.     case 'E':        /* Output Error */
  294.     case 'e':
  295.         break;
  296.  
  297.     case 'P':        /* Output P1 and P2 */
  298.     case 'p':
  299.         break;
  300.  
  301.     case 'S':        /* Output Status */
  302.     case 's':
  303.         break;
  304.  
  305.     case 'W':        /* Output Window (Not HP-GL) */
  306.     case 'w':
  307.         break;
  308.  
  309.     default:
  310.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  311.     }
  312.     break;
  313.  
  314.     case 'P':
  315.     case 'p':
  316.     op2 = getc(stream);
  317.     switch (op2)
  318.     {
  319.     case 'A':        /* Plot Absolute */
  320.     case 'a':
  321.         PLOTABS = 1;
  322.         if (SIGNED_NUMERIC)
  323.         if (SETDOT || SYMBOL)
  324.             plotdot(MOVE);
  325.         else if (PENDOWN)
  326.             plotps(DRAW);
  327.         else
  328.             plotps(MOVE);
  329.         break;
  330.  
  331.     case 'D':        /* Pen Down */
  332.     case 'd':
  333.         PENDOWN = 1;
  334.         if (SIGNED_NUMERIC)
  335.         if (SETDOT)
  336.             plotdot(MOVE);
  337.         else if (PLOTABS)
  338.             plotps(DRAW);
  339.         else 
  340.             plotps(RDRAW);
  341.         break;
  342.  
  343.     case 'R':        /* Plot Relative */
  344.     case 'r':
  345.         PLOTABS = 0;
  346.         if (SIGNED_NUMERIC)
  347.         if (SETDOT || SYMBOL)
  348.             plotdot(RMOVE);
  349.         else if (PENDOWN)
  350.             plotps(RDRAW);
  351.         else
  352.             plotps(RMOVE);
  353.         break;
  354.  
  355.     case 'T':        /* Pen Thickness (Not HP-GL) */
  356.     case 't':
  357.         {
  358.         float   linewidth;
  359.  
  360.         linewidth = getval() * SCALE;        /* In mm */
  361.         printf("%g mm setlinewidth\n", linewidth);
  362.         }
  363.         break;
  364.  
  365.     case 'U':        /* Pen Up */
  366.     case 'u':
  367.         PENDOWN = 0;
  368.         if (SIGNED_NUMERIC)
  369.         if (SETDOT)
  370.             plotdot(MOVE);
  371.         else if (PLOTABS)
  372.             plotps(MOVE);
  373.         else
  374.             plotps(RMOVE);
  375.         break;
  376.  
  377.     default:
  378.         fprintf(stderr, "Warning: %c%c Unknown HP-GL Command\n", op1, op2);
  379.     }
  380.     break;
  381.  
  382.     case 'R':            /* Not HP-GL */
  383.     case 'r':
  384.     op2 = getc(stream);
  385.     switch (op2)
  386.     {
  387.     case 'A':        /* Shade Rectange Absolute */
  388.     case 'a':
  389.         break;
  390.  
  391.     case 'R':